package com.web.portal.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.web.portal.pojo.Relation;
import com.web.portal.response.ResponseResult;
import com.web.portal.services.RelationService;
import com.web.portal.utils.IdWorker;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 关系
 */
@Slf4j
@RestController
@Api(tags = "关系管理")
@RequestMapping("/admin/relation")
public class RelationAdminApi {
    @Autowired
    private RelationService relationService;
    @Autowired
    private IdWorker idWorker;

    /**
     * 添加关系
     * 需要管理员权限
     *
     * @param relation
     * @return
     */
    @PostMapping
    public ResponseResult addRelation(@RequestBody Relation relation) {
        relation.setRelationCode(idWorker.nextId() + "");
        boolean flag = relationService.save(relation);
        if (flag) {
            return ResponseResult.SUCCESS("添加成功");
        } else {
            return ResponseResult.FAILED("添加失败");
        }
    }

    /**
     * 获取关系
     *
     * @param relationId
     * @return
     */
    @GetMapping("{relationId}")
    public ResponseResult getRelation(@PathVariable("relationId") String relationId) {
        Relation relation = relationService.getById(relationId);
        if (relation == null) {
            return ResponseResult.FAILED("查找失败");
        }
        relation.setRelationType(getRelationType(relation.getRelationType()));
        return ResponseResult.SUCCESS().setData(relation);
    }

    /**
     * 获取关系
     *
     * @param relationCode
     * @return
     */
    // test
    @GetMapping("by_relationCode/{relationCode}")
    public ResponseResult getRelationByRelationCode(@PathVariable("relationCode") String relationCode) {
        Relation relation = relationService.selectByRelationCode(relationCode);
        if (relation == null) {
            return ResponseResult.FAILED("查找失败");
        }
        relation.setRelationType(getRelationType(relation.getRelationType()));
        return ResponseResult.SUCCESS().setData(relation);
    }

    /**
     * 获取关系
     * <p>
     * 权限：管理员权限
     *
     * @return
     */
    @GetMapping("/list")
    public ResponseResult listRelation() {
        return ResponseResult.SUCCESS("获取分类列表成功.")
                .setData(relationService.list());
    }

    /**
     * 获取关系
     * <p>
     * 权限：管理员权限
     *
     * @return
     */
    @GetMapping("/{current}/{limit}")
    public ResponseResult findRelation(@PathVariable long current,
                                       @PathVariable long limit) {
        //创建page对象，传递当前页，每页记录数
        Page<Relation> page = new Page<>(current, limit);

        QueryWrapper<Relation> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        //调用方法实现分页查询
        Page<Relation> pageLabelInfo = relationService.page(page, wrapper);
        //返回结果
        return ResponseResult.SUCCESS("获取分类列表成功.")
                .setData(pageLabelInfo);
    }

    /**
     * 更新关系
     *
     * @param relation
     * @return
     */
    @PostMapping("updateActivity")
    public ResponseResult updateRelation(@RequestBody Relation relation) {
        boolean flag = relationService.updateById(relation);
        if (flag) {
            return ResponseResult.SUCCESS("更改成功");
        } else {
            return ResponseResult.FAILED("更改失败");
        }
    }

    /**
     * 关系
     *
     * @param relationId
     * @return
     */
    @DeleteMapping("/{activityId}")
    public ResponseResult deleteRelation(@PathVariable("activityId") String relationId) {
        boolean flag = relationService.removeById(relationId);
        if (flag) {
            return ResponseResult.SUCCESS("删除成功");
        } else {
            return ResponseResult.FAILED("删除失败");
        }
    }

    private String getRelationType(String relationType) {
        switch (relationType) {
            case "1":
                return "闺蜜";
            case "2":
                return "舍友";
            case "3":
                return "死党";
            default:
                return relationType;
        }
    }
}
